package com.dxl.lanju.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.enums.State;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.dao.StockDao;
import com.dxl.service.common.BaseService;
import com.lanju.bean.StockBean;
import com.lanju.entity.Area;
import com.lanju.entity.Product;
import com.lanju.entity.ProductSku;
import com.lanju.entity.Stock;
import com.lanju.entity.User;

@Service
@Transactional
public class StockService extends BaseService {
	private static final Logger LOG = Logger.getLogger(StockService.class);
	@Autowired
	private StockDao stockDao;
	
	public Page<StockBean> list(String areaId, String userId, String prodId, Integer state, Integer pageNo, Integer pageSize) {
		Page<StockBean> page = stockDao.list(areaId, userId, prodId, state, pageNo, pageSize);
		List<StockBean> rows = page.getRows();
		if (Utils.isNotEmpty(rows)) {
			String id;
			for (StockBean row : rows) {
				id = row.getAreaId();
				Area area = super.get(Area.class, id);
				if (Utils.isNotNull(area)) {
					row.setAreaName(area.getName());
				}
				
				id = row.getUserId();
				User user = super.get(User.class, id);
				if (Utils.isNotNull(user)) {
					row.setUsername(user.getName());
				}
				
				id = row.getProdId();
				Product product = super.get(Product.class, id);
				if (Utils.isNotNull(product)) {
					row.setProdName(product.getName());
					row.setProdImg(product.getPathSl());
				}
				
				id = row.getProdSkuId();
				ProductSku productSku = super.get(ProductSku.class, id);
				if (Utils.isNotNull(productSku)) {
					row.setProdSkuName(productSku.toSkuName());
					row.setUnit(productSku.getUnit());
					row.setUnitPrice(productSku.getUnitPrice());
					if (Utils.isNotNull(row.getFixedNum()) && Utils.isNotNull(productSku.getUnitPrice())) {
						int total = Utils.isNull(row.getAddNum()) ? row.getFixedNum() : row.getFixedNum() + row.getAddNum();
						row.setTotalPrice(productSku.getUnitPrice().multiply(new BigDecimal(total)));
					}
				}
			}
		}
		return page;
	}
	
	public RespData save(Stock stock) {
		Stock srcStock;
		if (Utils.isNotBlank(stock.getId())) {
			srcStock = this.get(Stock.class, stock.getId());
			srcStock.setAreaId(stock.getAreaId());
			srcStock.setUserId(stock.getUserId());
			srcStock.setProdSkuId(stock.getProdSkuId());
			srcStock.setFixedNum(stock.getFixedNum());
			srcStock.setAddNum(stock.getAddNum());
			srcStock.setRemark(stock.getRemark());
		} else {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", stock.getUserId());
			params.put("state", State.ACTIVE.getState());
			params.put("prodSkuId", stock.getProdSkuId());
			List<Stock> rows = stockDao.listByProps(Stock.class, params);
			if (Utils.isEmpty(rows)) {
				srcStock = stock;
			} else {
				srcStock = rows.get(0);
				srcStock.setAreaId(stock.getAreaId());
				srcStock.setUserId(stock.getUserId());
				srcStock.setProdSkuId(stock.getProdSkuId());
				srcStock.setFixedNum(stock.getFixedNum());
				srcStock.setAddNum(stock.getAddNum());
				srcStock.setRemark(stock.getRemark());
			}
		}
		ProductSku productSku = super.get(ProductSku.class, stock.getProdSkuId());
		if (Utils.isNotNull(productSku)) {
			srcStock.setProdId(productSku.getProdId());
		}
		super.save(srcStock);
		return RespData.instance(RespData.SUCCESS, srcStock.getId());
	}
}
